<?php
class Controller extends CController
{
	protected function beforeAction($action)
	{
		// Disable jQuery UI
		//Yii::app()->clientScript->corePackages = array();
		Yii::app()->clientScript->scriptMap = array(
			//'jquery.js'=>false,
			//'jquery.min.js'=>false,
			'jquery-ui.js'=>false,
			'jquery-ui.min.js'=>false,
			'jquery.ui.css'=>false,
			'jquery-ui.css'=>false,
		);
		// Define IMAGES_URL
		$baseUrl = Yii::app()->getBaseUrl(true);
		define ('IMAGES_URL', $baseUrl . '/images/' );
		return parent::beforeAction($action);
	}

	// Default variables
	public $layout='//layouts/default';
	public $menu=array();
	public $breadcrumbs=array();

	// SEO Variables
	public $pageTitle = SITE_TITLE;
	public $pageDescription = SITE_DESCRIPTION;
	public $pageAuthor = SITE_AUTHOR;
	public $pageKeywords = SITE_KEYWORDS;
	public $pageCanonical = '/Home/default/index';
	public $pageRobotsIndex = 'index';
	public $pageRobotsFollow = 'follow';

    public $pageFacebook=array(
		'og:title'=>SITE_TITLE,
		'og:author'=>SITE_AUTHOR,
		'og:type'=>'article',
		'og:image'=>SITE_IMAGE,
		'og:url'=>'/Home/default/index',
		'og:description'=>SITE_DESCRIPTION,
		'og:locale'=>'vi_VN',
	);

	public $pageTwitter=array(
		'twitter:card'=>'summary',
		'twitter:site'=>'@NEXTTOPEVENT',
		'twitter:domain'=>'sự kiện nexttopevent',
		'twitter:creator'=>'@NEXTTOPEVENT',
	);

	public function displayMetaTags()
	{
		// Standard Meta
		echo "\t".''.PHP_EOL;
		echo "\t".'<meta http-equiv="content-language" content="vi" />'.PHP_EOL;
		echo "\t".'<meta http-equiv="audience" content="General" />'.PHP_EOL;
		echo "\t".'<title>'.$this->pageTitle.'</title>'.PHP_EOL;
		echo "\t".'<meta name="description" content="',CHtml::encode($this->pageDescription),'">'.PHP_EOL;
		echo "\t".'<meta name="author" content="',CHtml::encode($this->pageAuthor),'">'.PHP_EOL;
		echo "\t".'<meta name="keywords" content="',CHtml::encode($this->pageKeywords),'">'.PHP_EOL;
		echo "\t".'<meta name="distribution" content="Global" />'.PHP_EOL;
		echo "\t".'<meta name="generator" content="Nexttopevent.com" />'.PHP_EOL;
		echo "\t".'<meta name="copyright" content="Công ty cổ phần DnD Viet nam" />'.PHP_EOL;
		echo "\t".'<meta name="revisit-after" content="1 days" />'.PHP_EOL;
		echo "\t".'<meta name="RATING" content="GENERAL" />'.PHP_EOL;

		// Option for NoIndex
		echo '<meta name="robots" content="',CHtml::encode($this->pageRobotsIndex),', ',CHtml::encode($this->pageRobotsFollow),'">'.PHP_EOL;

		// Facebook Meta
		foreach ($this->pageFacebook as $key=>$pageFacebook) {
			echo "\t".'<meta property="' . $key . '" content="';
			if ($key === 'og:image') {
				 echo IMAGES_URL . CHtml::encode($pageFacebook) . '">'.PHP_EOL;
			}
			elseif ($key === 'og:url') {
                if (!is_array($pageFacebook)) {
                    echo Yii::app()->createAbsoluteUrl($pageFacebook) . '">'.PHP_EOL;
                }
                else {
                    echo Yii::app()->createAbsoluteUrl($pageFacebook['0'], $pageFacebook['1']) . '">'.PHP_EOL;
                }
			}
			else {
				echo CHtml::encode($pageFacebook) . '">'.PHP_EOL;
			}
		}
		echo "\t".'<meta property="fb:admins" content="' . FACEBOOK_ID . ' ">'.PHP_EOL;
		echo "\t".'<meta property="fb:app_id" content="266056400216595"/>'.PHP_EOL;
		// Twitter Meta
		foreach ($this->pageTwitter as $key=>$pageTwitter) {
			echo "\t".'<meta property="' . $key . '" content="' . CHtml::encode($pageTwitter) . '">'.PHP_EOL;
		}
		// Google Meta
		echo "\t".'<link rel="author" href="https://plus.google.com/113102258271835960236/about" />'.PHP_EOL;
		echo "\t".'<link rel="publisher" href="https://plus.google.com/b/' . GOOGLE_ID . '"/>'.PHP_EOL;
		//https://plus.google.com/113102258271835960236/about
		// Canonical Meta
		if (is_array($this->pageCanonical)) {
			echo "\t".'<link rel="canonical" href="'.Yii::app()->createAbsoluteUrl($this->pageCanonical[0], $this->pageCanonical[1]).'">'.PHP_EOL;
		}
		else {
			echo "\t".'<link rel="canonical" href="'.Yii::app()->createAbsoluteUrl($this->pageCanonical).'">'.PHP_EOL;
		}
	}

	private $utfSign = array(
		'/a/', '/á/', '/à/', '/ả/', '/ạ/', '/ã/',
		'/ă/', '/ắ/', '/ằ/', '/ẳ/',	'/ặ/', '/ẵ/',
		'/â/', '/ấ/', '/ầ/', '/ẩ/', '/ậ/', '/ẫ/',
		'/e/', '/é/', '/è/', '/ẻ/', '/ẹ/', '/ẽ/',
		'/ê/', '/ế/', '/ề/', '/ể/', '/ệ/', '/ễ/',
		'/i/', '/í/', '/ì/', '/ỉ/', '/ị/', '/ĩ/',
		'/o/', '/ó/', '/ò/', '/ỏ/', '/ọ/', '/õ/',
		'/ô/', '/ố/', '/ồ/', '/ổ/', '/ộ/', '/ỗ/',
		'/ơ/', '/ớ/', '/ờ/', '/ở/', '/ợ/', '/ỡ/',
		'/u/', '/ú/', '/ù/', '/ủ/', '/ụ/', '/ũ/',
		'/ư/', '/ứ/', '/ừ/', '/ử/', '/ự/', '/ữ/',
		'/y/', '/ý/', '/ỳ/', '/ỷ/', '/ỵ/', '/ỹ/',
		'/đ/',
		'/A/', '/À/', '/Á/', '/Ạ/', '/Ả/', '/Ã/',
		'/Â/', '/Ầ/', '/Ấ/', '/Ậ/', '/Ẩ/', '/Ẫ/',
		'/Ă/', '/Ằ/', '/Ắ/', '/Ặ/', '/Ẳ/', '/Ẵ/',
		'/E/', '/È/', '/É/', '/Ẹ/', '/Ẻ/', '/Ẽ/',
		'/Ê/', '/Ề/', '/Ế/', '/Ệ/', '/Ể/', '/Ễ/',
		'/I/', '/Ì/', '/Í/', '/Ị/', '/Ỉ/', '/Ĩ/',
		'/O/', '/Ò/', '/Ó/', '/Ọ/', '/Ỏ/', '/Õ/',
		'/Ô/', '/Ồ/', '/Ố/', '/Ộ/', '/Ổ/', '/Ỗ/',
		'/Ơ/', '/Ờ/', '/Ớ/', '/Ợ/', '/Ở/', '/Ỡ/',
		'/U/', '/Ù/', '/Ú/', '/Ụ/', '/Ủ/', '/Ũ/',
		'/Ư/', '/Ừ/', '/Ứ/', '/Ự/', '/Ử/', '/Ữ/',
		'/Y/', '/Ỳ/', '/Ý/', '/Ỵ/', '/Ỷ/', '/Ỹ/',
		'/Đ/',
		// Unknown character
		'/̉/', '/̀/', '/́/', '/̉/', '/̣/', '/̃/',
		'/"/', '/\'/', '/_/', '/“/','/”/'
	);

	private $utfNoneSign = array(
		'a', 'a', 'a', 'a', 'a', 'a',
		'a', 'a', 'a', 'a', 'a', 'a',
		'a', 'a', 'a', 'a', 'a', 'a',
		'e', 'e', 'e', 'e', 'e', 'e',
		'e', 'e', 'e', 'e', 'e', 'e',
		'i', 'i', 'i', 'i', 'i', 'i',
		'o', 'o', 'o', 'o', 'o', 'o',
		'o', 'o', 'o', 'o', 'o', 'o',
		'o', 'o', 'o', 'o', 'o', 'o',
		'u', 'u', 'u', 'u', 'u', 'u',
		'u', 'u', 'u', 'u', 'u', 'u',
		'y', 'y', 'y', 'y', 'y', 'y',
		'd',
		'A', 'A', 'A', 'A', 'A', 'A',
		'A', 'A', 'A', 'A', 'A', 'A',
		'A', 'A', 'A', 'A', 'A', 'A',
		'E', 'E', 'E', 'E', 'E', 'E',
		'E', 'E', 'E', 'E', 'E', 'E',
		'I', 'I', 'I', 'I', 'I', 'I',
		'O', 'O', 'O', 'O', 'O', 'O',
		'O', 'O', 'O', 'O', 'O', 'O',
		'O', 'O', 'O', 'O', 'O', 'O',
		'U', 'U', 'U', 'U', 'U', 'U',
		'U', 'U', 'U', 'U', 'U', 'U',
		'Y', 'Y', 'Y', 'Y', 'Y', 'Y',
		'D',
		// Unknown character
		'', '', '', '', '', '',
		'', '', '', '', ''
	);

	public function formatUrl($value)
	{
		$strOut = preg_replace("/([\s]+)/","-",strtolower(trim($value)));
		$strOut = preg_replace("/([\W]+)/","-",strtolower(trim($strOut )));
		$strOut = preg_replace("/--/","-",strtolower(trim($strOut )));
		return $strOut;
	}

	public function convertString($value, $opt=0)
	{
		$r_value ="";
		if (!empty($value))
		{
			$r_value = trim(preg_replace($this->utfSign, $this->utfNoneSign, $value));
		}
		if ($opt == 1) {
			$r_value = trim(preg_replace('/\\\|\'/', '', $r_value));
		}
		if ($opt == 2) {
			$r_value = trim(preg_replace('/"/', '&quot;', $r_value));
		}
		return $r_value;
	}

	public function createSlug($value)
	{
		return $this->formatUrl($this->convertString($value, 1));
	}

	public function resizeImage($imagePath, $imageName, $postedTime)
	{
		// Set file out
		$ext = pathinfo($imageName, PATHINFO_EXTENSION);
		$name = pathinfo($imageName, PATHINFO_FILENAME);
		$dir = date('Ymd', strtotime($postedTime));
		// Create new Object
		$thumb = new Imagick();
		// Set Options
		$thumb->readImage($imagePath . $imageName);
		$thumb->setCompressionQuality(90);

		// Get current Image size
		$size = $thumb->getImageGeometry();
		// Resize #1: Set the original images
		$dest = IMAGES_DIR . 'events/primary/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		if ($size['width'] > 1024 || $size['height'] > 576) {
			$thumb->resizeImage(1024, 576, Imagick::FILTER_LANCZOS, 1, true);
			$thumb->writeImage($dest . $imageName);
		}
		else {
			$thumb->writeImage($dest . $imageName);
		}
		// Resize #2
		$dest = IMAGES_DIR . 'events/slides/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		$thumb->resizeImage(520, 285, Imagick::FILTER_LANCZOS, 1);
		$thumb->writeImage($dest . $imageName);
		// Resize #3
		$dest = IMAGES_DIR . 'events/thumbs/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		$thumb->resizeImage(160, 90, Imagick::FILTER_LANCZOS, 1);
		$thumb->writeImage($dest . $imageName);
		// Resize Clear
		$thumb->clear();
		$thumb->destroy();
	}

	public function resizeChartImage($imagePath, $imageName, $postedTime)
	{
		// Set file out
		$ext = pathinfo($imageName, PATHINFO_EXTENSION);
		$name = pathinfo($imageName, PATHINFO_FILENAME);
		$dir = date('Ymd', strtotime($postedTime));
		// Create new Object
		$thumb = new Imagick();
		// Set Options
		$thumb->readImage($imagePath . $imageName);
		$thumb->setCompressionQuality(90);

		// Get current Image size
		$size = $thumb->getImageGeometry();
		// Resize
		$dest = IMAGES_DIR . 'events/charts/' . $dir . '/';
		if (!file_exists($dest)) {
			mkdir($dest, 0777, true);
		}
		if ($size['width'] > 1024 || $size['height'] > 576) {
			$thumb->resizeImage(1024, 576, Imagick::FILTER_LANCZOS, 1, true);
			$thumb->writeImage($dest . $imageName);
		}
		else {
			$thumb->writeImage($dest . $imageName);
		}
		// Resize Clear
		$thumb->clear();
		$thumb->destroy();
	}
}